home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / DELPHI32 / COMPNENT / SAWIN95 / TRAYICON.PAS < prev    next >
Pascal/Delphi Source File  |  1996-10-22  |  5KB  |  188 lines

  1. unit TrayIcon;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Menus,
  7.   WComp, ExtCtrls, ShellAPI;
  8.  
  9. type
  10.   TTrayIcon = class(TWindowedComponent)
  11.   private
  12.     { property variables }
  13.     FActive      : Boolean;
  14.     FIcon        : TIcon;
  15.     FHint        : String;
  16.     FPopupMenu   : TPopupMenu;
  17.     { event variables }
  18.     FOnClick     : TNotifyEvent;
  19.     FOnDblClick  : TNotifyEvent;
  20.     { internal variables }
  21.     FCallBackMsg : Word;
  22.     FNotifyData  : TNotifyIconData;
  23.     FPreventClick: Boolean;
  24.     FTrayIcon    : hIcon;
  25.     FTimer       : TTimer;
  26.     { Property setting routines }
  27.     procedure SetActive(Value: Boolean);
  28.     procedure SetIcon(Value: TIcon);
  29.     procedure SetHint(Value: String);
  30.     procedure SetPopupMenu(Value: TPopupMenu);
  31.   protected
  32.     { Internal routines }
  33.     procedure ShellNotifyIcon(Msg: DWord; Flags: UInt);
  34.     procedure OnClickTimer(Sender: TObject);
  35.     function  LoadWorldIcon: THandle;
  36.     { Overrides }
  37.     procedure WndProc(var Message: TMessage); override;
  38.     procedure Notification(AComponent: TComponent; AOperation: TOperation); override;
  39.     procedure Loaded; override;
  40.   public
  41.     { Public routines }
  42.     constructor Create(AOwner: TComponent); override;
  43.     destructor  Destroy; override;
  44.   published
  45.     { Properties }
  46.     property Active    : Boolean      read FActive     write SetActive default False;
  47.     property Icon      : TIcon        read FIcon       write SetIcon;
  48.     property Hint      : String       read FHint       write SetHint;
  49.     property PopupMenu : TPopupMenu   read FPopupMenu  write FPopupMenu;
  50.     { Events }
  51.     property OnClick   : TNotifyEvent read FOnClick    write FOnClick;
  52.     property OnDblClick: TNotifyEvent read FOnDblClick write FOnDblClick;
  53.   end;
  54.  
  55. {$R TRAYICON.RES}
  56.  
  57. implementation
  58.  
  59. { TTrayIcon }
  60.  
  61. constructor TTrayIcon.Create(AOwner: TComponent);
  62. begin
  63.   inherited Create(AOwner);
  64.   FCallbackMsg := RegisterWindowMessage('TTrayIconCallBackMsg');
  65.   FIcon := TIcon.Create;
  66.   FIcon.Handle := LoadWorldIcon;
  67.   FTimer := TTimer.Create(Self);
  68.   with FTimer do begin
  69.     Enabled := False;
  70.     Interval := GetDoubleClickTime;
  71.     OnTimer := OnClickTimer;
  72.   end;
  73. end;
  74.  
  75. destructor TTrayIcon.Destroy;
  76. begin
  77.   Active := False;
  78.   FIcon.Free;
  79.   FTimer.Free;
  80.   inherited Destroy;
  81. end;
  82.  
  83. procedure TTrayIcon.Loaded;
  84. begin
  85.   inherited Loaded;
  86.   if Active then ShellNotifyIcon(NIM_ADD, NIF_MESSAGE or NIF_ICON or NIF_TIP);
  87. end;
  88.  
  89. procedure TTrayIcon.WndProc(var Message: TMessage);
  90. var
  91.   Point: TPoint;
  92. begin
  93.   with Message do
  94.    begin
  95.      if (Msg = FCallBackMsg) and (wParam = 0) then
  96.       begin
  97.         case lParam of
  98.  
  99.           wm_LButtonDown   : FTimer.Enabled := True;
  100.           wm_LButtonDblClk :
  101.            begin
  102.              FPreventClick := True;
  103.              if Assigned(OnDblClick) then OnDblClick(Self);
  104.            end;
  105.           wm_RButtonDown   :
  106.            begin
  107.              if Assigned(PopupMenu) then
  108.               begin
  109.                 SetForeGroundWindow((Owner As TForm).Handle);
  110.                 GetCursorPos(Point);
  111.                 PopupMenu.Popup(Point.X, Point.Y);
  112.                 PostMessage((Owner As TForm).Handle, WM_USER, 0, 0);
  113.               end;
  114.            end;
  115.         end;
  116.       end
  117.      else
  118.       inherited;
  119.    end;
  120. end;
  121.  
  122. procedure TTrayIcon.SetActive(Value: Boolean);
  123. const
  124.   Values: array[Boolean] of DWord = (NIM_DELETE, NIM_ADD);
  125. begin
  126.   if FActive <> Value then
  127.    begin
  128.      FActive := Value;
  129.      ShellNotifyIcon(Values[Active], NIF_MESSAGE or NIF_ICON or NIF_TIP);
  130.    end;
  131. end;
  132.  
  133. procedure TTrayIcon.SetHint(Value : String);
  134. begin
  135.   if FHint <> Value then
  136.    begin
  137.      FHint := Value;
  138.      if Active then ShellNotifyIcon(NIM_MODIFY, NIF_TIP);
  139.    end;
  140. end;
  141.  
  142. procedure TTrayIcon.SetIcon(Value: TIcon);
  143. begin
  144.   FIcon.Assign(Value);
  145.   if FIcon.Empty then FIcon.Handle := LoadWorldIcon;
  146.   if Active then ShellNotifyIcon(NIM_MODIFY, NIF_ICON);
  147. end;
  148.  
  149. procedure TTrayIcon.SetPopupMenu(Value: TPopupMenu);
  150. begin
  151.   FPopupMenu := Value;
  152.   if Value <> nil then Value.FreeNotification(Self);
  153. end;
  154.  
  155. procedure TTrayIcon.Notification(AComponent: TComponent; AOperation: TOperation);
  156. begin
  157.   inherited Notification(AComponent, AOperation);
  158.   if (AComponent = PopupMenu) and (AOperation = opRemove) then PopupMenu := nil;
  159. end;
  160.  
  161. procedure TTrayIcon.OnClickTimer(Sender: TObject);
  162. begin
  163.   FTimer.Enabled := False;
  164.   if (not FPreventClick) and Assigned(OnClick) then OnClick(Self);
  165.   FPreventClick := False;
  166. end;
  167.  
  168. function TTrayIcon.LoadWorldIcon: THandle;
  169. begin
  170.   Result := LoadImage(hInstance, 'TRAYICON', IMAGE_ICON, 16, 16, LR_LOADREALSIZE);
  171. end;
  172.  
  173. procedure TTrayIcon.ShellNotifyIcon(Msg: DWord; Flags: UInt);
  174. begin
  175.   with FNotifyData do begin
  176.     cbSize := SizeOf(FNotifyData);
  177.     StrPLCopy(szTip, PChar(Hint), SizeOf(szTip));
  178.     uFlags := Flags;
  179.     uID := 0;
  180.     Wnd := Handle;
  181.     uCallbackMessage := FCallBackMsg;
  182.     hIcon  := FIcon.Handle;
  183.   end;
  184.   Shell_NotifyIcon(Msg, @FNotifyData);
  185. end;
  186.  
  187. end.
  188.